﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;

namespace HY.DAL.CO
{
    /// <summary>
    /// 数据访问类:comp_tel
    /// </summary>
    public partial class comp_tel
    {
        public comp_tel()
        { }
        #region  Method
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int sid)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from comp_tel");
            strSql.Append(" where sid=@sid");
            SqlParameter[] parameters = {
					new SqlParameter("@sid", SqlDbType.Int,4)
			};
            parameters[0].Value = sid;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(HY.Model.CO.comp_tel model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into comp_tel(");
            strSql.Append("pbh,pname,cbh,cname,tel,person,pmemo,pdtime,sdtime)");
            strSql.Append(" values (");
            strSql.Append("@pbh,@pname,@cbh,@cname,@tel,@person,@pmemo,@pdtime,@sdtime)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@pbh", SqlDbType.NVarChar,100),
					new SqlParameter("@pname", SqlDbType.NVarChar,100),
					new SqlParameter("@cbh", SqlDbType.NVarChar,100),
					new SqlParameter("@cname", SqlDbType.NVarChar,100),
					new SqlParameter("@tel", SqlDbType.NVarChar,500),
					new SqlParameter("@person", SqlDbType.NVarChar,500),
					new SqlParameter("@pmemo", SqlDbType.NVarChar,500),
					new SqlParameter("@pdtime", SqlDbType.DateTime),
					new SqlParameter("@sdtime", SqlDbType.DateTime)};
            parameters[0].Value = model.pbh;
            parameters[1].Value = model.pname;
            parameters[2].Value = model.cbh;
            parameters[3].Value = model.cname;
            parameters[4].Value = model.tel;
            parameters[5].Value = model.person;
            parameters[6].Value = model.pmemo;
            parameters[7].Value = model.pdtime;
            parameters[8].Value = model.sdtime;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(HY.Model.CO.comp_tel model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update comp_tel set ");
            strSql.Append("pbh=@pbh,");
            strSql.Append("pname=@pname,");
            strSql.Append("cbh=@cbh,");
            strSql.Append("cname=@cname,");
            strSql.Append("tel=@tel,");
            strSql.Append("person=@person,");
            strSql.Append("pmemo=@pmemo,");
            strSql.Append("pdtime=@pdtime,");
            strSql.Append("sdtime=@sdtime");
            strSql.Append(" where sid=@sid");
            SqlParameter[] parameters = {
					new SqlParameter("@pbh", SqlDbType.NVarChar,100),
					new SqlParameter("@pname", SqlDbType.NVarChar,100),
					new SqlParameter("@cbh", SqlDbType.NVarChar,100),
					new SqlParameter("@cname", SqlDbType.NVarChar,100),
					new SqlParameter("@tel", SqlDbType.NVarChar,500),
					new SqlParameter("@person", SqlDbType.NVarChar,500),
					new SqlParameter("@pmemo", SqlDbType.NVarChar,500),
					new SqlParameter("@pdtime", SqlDbType.DateTime),
					new SqlParameter("@sdtime", SqlDbType.DateTime),
					new SqlParameter("@sid", SqlDbType.Int,4)};
            parameters[0].Value = model.pbh;
            parameters[1].Value = model.pname;
            parameters[2].Value = model.cbh;
            parameters[3].Value = model.cname;
            parameters[4].Value = model.tel;
            parameters[5].Value = model.person;
            parameters[6].Value = model.pmemo;
            parameters[7].Value = model.pdtime;
            parameters[8].Value = model.sdtime;
            parameters[9].Value = model.sid;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int sid)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from comp_tel ");
            strSql.Append(" where sid=@sid");
            SqlParameter[] parameters = {
					new SqlParameter("@sid", SqlDbType.Int,4)
			};
            parameters[0].Value = sid;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string sidlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from comp_tel ");
            strSql.Append(" where sid in (" + sidlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public HY.Model.CO.comp_tel GetModel(int sid)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 sid,pbh,pname,cbh,cname,tel,person,pmemo,pdtime,sdtime from comp_tel ");
            strSql.Append(" where sid=@sid");
            SqlParameter[] parameters = {
					new SqlParameter("@sid", SqlDbType.Int,4)
			};
            parameters[0].Value = sid;

            HY.Model.CO.comp_tel model = new HY.Model.CO.comp_tel();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["sid"] != null && ds.Tables[0].Rows[0]["sid"].ToString() != "")
                {
                    model.sid = int.Parse(ds.Tables[0].Rows[0]["sid"].ToString());
                }
                if (ds.Tables[0].Rows[0]["pbh"] != null && ds.Tables[0].Rows[0]["pbh"].ToString() != "")
                {
                    model.pbh = ds.Tables[0].Rows[0]["pbh"].ToString();
                }
                if (ds.Tables[0].Rows[0]["pname"] != null && ds.Tables[0].Rows[0]["pname"].ToString() != "")
                {
                    model.pname = ds.Tables[0].Rows[0]["pname"].ToString();
                }
                if (ds.Tables[0].Rows[0]["cbh"] != null && ds.Tables[0].Rows[0]["cbh"].ToString() != "")
                {
                    model.cbh = ds.Tables[0].Rows[0]["cbh"].ToString();
                }
                if (ds.Tables[0].Rows[0]["cname"] != null && ds.Tables[0].Rows[0]["cname"].ToString() != "")
                {
                    model.cname = ds.Tables[0].Rows[0]["cname"].ToString();
                }
                if (ds.Tables[0].Rows[0]["tel"] != null && ds.Tables[0].Rows[0]["tel"].ToString() != "")
                {
                    model.tel = ds.Tables[0].Rows[0]["tel"].ToString();
                }
                if (ds.Tables[0].Rows[0]["person"] != null && ds.Tables[0].Rows[0]["person"].ToString() != "")
                {
                    model.person = ds.Tables[0].Rows[0]["person"].ToString();
                }
                if (ds.Tables[0].Rows[0]["pmemo"] != null && ds.Tables[0].Rows[0]["pmemo"].ToString() != "")
                {
                    model.pmemo = ds.Tables[0].Rows[0]["pmemo"].ToString();
                }
                if (ds.Tables[0].Rows[0]["pdtime"] != null && ds.Tables[0].Rows[0]["pdtime"].ToString() != "")
                {
                    model.pdtime = DateTime.Parse(ds.Tables[0].Rows[0]["pdtime"].ToString());
                }
                if (ds.Tables[0].Rows[0]["sdtime"] != null && ds.Tables[0].Rows[0]["sdtime"].ToString() != "")
                {
                    model.sdtime = DateTime.Parse(ds.Tables[0].Rows[0]["sdtime"].ToString());
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select sid,pbh,pname,cbh,cname,tel,person,pmemo,pdtime,sdtime ");
            strSql.Append(" FROM comp_tel ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" sid,pbh,pname,cbh,cname,tel,person,pmemo,pdtime,sdtime ");
            strSql.Append(" FROM comp_tel ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM comp_tel ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.sid desc");
            }
            strSql.Append(")AS Row, T.*  from comp_tel T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "comp_tel";
            parameters[1].Value = "sid";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  Method
    }
}

